﻿using MC.Warehouse.Core;
using MC.Warehouse.DAL;
using MC.Warehouse.Models;
using System.ComponentModel;
using System.Linq;
using System.Windows.Data;

namespace MC.Warehouse.ViewModels
{
    public class CustomerListViewModel : UserControlViewModel
    {
        #region CONTRUCTOR
        
        public CustomerListViewModel()
        {
            Initialize();        
        }

        #endregion

        #region USERCONTROL CORE FUNCTION
        
        internal override sealed void Initialize()
        {
            Model = new CustomerModel();
            Refresh();
        }

        internal override void Refresh()
        {
            using (var repository = new CustomersRepository())
            {
                //WaitingProcess.ShowSplashScreen();
                DataSources = repository.AutoLoad();
                //WaitingProcess.CloseForm();
            }
        }

        internal override void Search()
        {
            using (var repository = new CustomersRepository())
            {
                //WaitingProcess.ShowSplashScreen();
                DataSources = repository.Search(_model);
                //WaitingProcess.CloseForm();
            }
        }

        internal override void Clean()
        {
            //WaitingProcess.ShowSplashScreen();
            Model = new CustomerModel();
            DataSources = CollectionViewSource.GetDefaultView(null);
            //WaitingProcess.CloseForm();
        }

        internal override void New()
        {
            //WaitingProcess.ShowSplashScreen();
            TabbarManager.OpenTab(UcPartialName, UcPartialCaption);
            var userControlViewModel = TabbarManager.UcSelected.DataContext as UserControlViewModel;
            if (userControlViewModel != null)
                userControlViewModel.New();
            //WaitingProcess.CloseForm();
        }

        internal override void Copy()
        {
            TabbarManager.OpenTab(UcPartialName, UcPartialCaption);
            var view = TabbarManager.UcSelected.DataContext as UserControlViewModel;
            if (view == null) return;
            view.Model = SerializeHelper.CloneObject((CustomerModel)DataSources.CurrentItem);
            view.Copy();
        }

        internal override void Delete()
        {
            var item = DataSources.CurrentItem as CustomerModel;
            var data = DataSources.Cast<CustomerModel>().ToList();
            data.Remove(item);
            DataSources = CollectionViewSource.GetDefaultView(data);
        }

        internal override void Edit()
        {
            if (DataSources.CurrentItem == null) return;
            TabbarManager.OpenTab(UcPartialName, UcPartialCaption);
            var newModel = SerializeHelper.CloneObject((CustomerModel)DataSources.CurrentItem);
            var userControlViewModel = TabbarManager.UcSelected.DataContext as UserControlViewModel;
            if (userControlViewModel != null)
                userControlViewModel.Model = newModel;
        }

        #endregion

        #region PROPERTIES AND DATA FIELD

        private CustomerModel _model;
        private ICollectionView _listResult;
        private const string UcPartialName = "CustomerView";
        private const string UcPartialCaption = "Customer";

        public ICollectionView DataSources
        {
            get
            {
                return _listResult;
            }
            set
            {
                _listResult = value;
                NotifyPropertyChanged(() => DataSources);
            }
        }

        public override BaseModel Model
        {
            get
            {
                return _model;
            }
            set
            {
                _model = value as CustomerModel;
                NotifyPropertyChanged(()=>Model);
            }
        }
        #endregion
    }
}
